home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 5⁄4⁄90 / 0119-Object Cache Macros-Apr90 < prev    next >
Encoding:
Text File  |  1990-05-04  |  2.9 KB  |  128 lines  |  [TEXT/GEOL]

  1. Item    2621463                         30-April-90        15:34PDT
  2.  
  3. From:   D0532                           Aidea Systems, Don Park,PRT
  4.  
  5. To:     CPLUS.DEV$                      C++ Interest List--Developers
  6.         CPLUS.APPLE$                    C++ Interest List--Apple Employees
  7.  
  8. Sub:    Object Cache Macros
  9.  
  10. I agree with you all about value parameter issue.  To beg forgiveness for being
  11. wrong and thus providing evidence to those who claim that programmers are not
  12. deities, here is hack I wrote while back.  It is a set of macro that allows
  13. objects to be cached.
  14.  
  15. First, here is the header file.  There is no implementation file.
  16.  
  17. #ifndef__CACHE__
  18. #define__CACHE__
  19.  
  20. #include <Generic.h>   // name2() macro
  21.  
  22. // Macro used to define name of cache class for given data type.
  23.  
  24. #defineCache(TYPE) name2(CacheOf,TYPE)
  25. #defineCacheDeclare(TYPE)      \
  26.    class Cache(TYPE)            \
  27.    {         \
  28.    public:       \
  29.    static TYPE *          \
  30.    Get ( void )       \
  31.    {         \
  32.    TYPE * dp;        \
  33.    if (dp = cache)         \
  34.    cache = *(TYPE**)dp;      \
  35.    return dp;        \
  36.    }              \
  37.    static void       \
  38.    Put ( TYPE * dp )         \
  39.    {              \
  40.    *(TYPE**)dp = cache;       \
  41.    cache = dp;       \
  42.    }               \
  43.    private:       \
  44.    static TYPE *   cache;        \
  45.    }
  46. #defineCacheImplement(TYPE)    \
  47.    TYPE * Cache(TYPE)::cache = 0L
  48.  
  49. #endif __CACHE__
  50.  
  51.  
  52. Here is an usage example:
  53.  
  54. #include <StdLib.h>
  55. #include <Stream.h>
  56. #include <Events.h>
  57. #include <Memory.h>
  58. #include "Cache.h"
  59.  
  60. class IntObj;
  61.  
  62. CacheDeclare(IntObj);
  63. CacheImplement(IntObj);
  64.  
  65. class IntObj
  66. {
  67. public:
  68.         void *
  69.     operator new ( size_t sz )
  70.     {
  71.         void * dp;
  72.  
  73.         if (!(dp = Cache(IntObj)::Get()))
  74.             dp = malloc(sz);
  75.         return dp;
  76.     }
  77.         void
  78.     operator delete ( void * dp )
  79.     {
  80.         Cache(IntObj)::Put((IntObj*)dp);
  81.     }
  82. private:
  83.     inti;
  84. };
  85.  
  86. const NI = 100;
  87. const NJ = 100;
  88.  
  89. IntObj *    iobj[NJ];
  90.  
  91. main ()
  92. {
  93.     long    tick;
  94.     int     i, j;
  95.     void *  dp;
  96.  
  97.     tick = TickCount();
  98.  
  99.     for (i = 0; i < NI; i++)
  100.     {
  101.         for (j = 0; j < NJ; j++)
  102.             iobj[j] = new IntObj;
  103.         for (j = 0; j < NJ; j++)
  104.             delete iobj[j];
  105.     }
  106.     // Flush cached IntObj free store blocks
  107.     while (dp = Cache(IntObj)::Get())
  108.         free(dp);
  109.  
  110.     tick = TickCount() - tick;
  111.     cout << "Cache Free Store = " << tick;
  112.     return 0;
  113. }
  114.  
  115. The reason I am sharing this with you guys is that this interesting set of
  116. macros allow very fast object allocation.  Above example program will allocate
  117. and destroy 10,000 tiny objects in 7 to 8 ticks compared to over 100 ticks for
  118. not using it.  While there are many different ways to implement caching in OOP,
  119. inheritance, reference, etc., use of separate class with static function
  120. members seems to be quite efficient and flexible to meet most requirements.
  121.  
  122. Hope you enjoy them.
  123.  
  124. Don Park
  125.  
  126. P.S. Usage of malloc/free is not important to the scheme.
  127.  
  128.